/* Copyright (C) 2008  Romolo Camplani, Marco Fortunato, Marco Marelli, Guido Rota, Fabio A. Schreiber et al.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
/*
 * InputInterface.java
 *
 * Created on October 30, 2007, 8:51 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.dei.perla.utils.emulator.runtime;


import java.util.*;
import java.io.*;

import org.dei.perla.utils.emulator.model.node.Node;
/**
 *
 * @author Adnan
 */
public class InputInterface {
    
    Scanner in;
    BufferedReader br;
    String line;
    ArrayList<String> result;
    InputInterfaceModelDescriptionBuilder iimdb;
    /** Creates a new instance of InputInterface */
    public InputInterface() {
        
        in = new Scanner(System.in);
        iimdb = new ModelDescriptionBuilder();
    }
    
    public ArrayList<Node> go()
    {
        /*
        Node n1 = new Node();
        Node n2 = new Node("2","-1",new Position(5,5),5.0);
        Position pos = n1.getPosition();
        System.out.println("x = "+pos.getX()+", y = "+pos.getY());
        pos = n2.getPosition();
        System.out.println("x = "+pos.getX()+", y = "+pos.getY());
        
        n2.sendData(new String("Charge 5.5"));
        String s1  = n2.getData("Charge");
        System.out.println("Charge is " + s1 );
    
        String s2  = n1.getData("Charge");
        System.out.println("Charge is " + s2 );
         */
        while(true)
        {
        System.out.println("Commands");
        System.out.println("1. To create node: \"create node default\"");
        System.out.println("2. To create node with values: \"create node value <nodeID> <baseStationID> <PosX> <PosY> <charge>\"");
        System.out.println("3. To show node values: \"show node <nodeID>\"");
        System.out.println("4. To change the charge of a node: \"set charge <nodeID> <value>\"");
        System.out.println("5. To show the charge of a node: \"show charge <nodeID>\"");
        System.out.println("6. To quit: \"quit\"");
        
        try{
        br = new BufferedReader(new InputStreamReader(System.in));
        line = br.readLine();
   //     result = null;
       String temp[];
       temp = line.split(" ");
       result = new ArrayList<String>();
       for(int i=0; i<temp.length; i++)
       {  
           result.add(temp[i]);
       }
        if (result.get(0).equalsIgnoreCase("create")
        && (result.get(1).equalsIgnoreCase("node"))
        && (result.get(2).equalsIgnoreCase("value"))
        )
            iimdb.createNode(result.get(3),result.get(4), Integer.parseInt(result.get(5)), Integer.parseInt(result.get(6)),Double.parseDouble(result.get(7)));    
        
            
        else  if (result.get(0).equalsIgnoreCase("create")
        && result.get(1).equalsIgnoreCase("node")
        && result.get(2).equalsIgnoreCase("default")
        )
            iimdb.createNode();
       
        else  if (result.get(0).equalsIgnoreCase("show")
        && result.get(1).equalsIgnoreCase("node")
        )
            iimdb.showNode(result.get(2));
        
        else if (result.get(0).equalsIgnoreCase("set")
        && result.get(1).equalsIgnoreCase("charge"))
             iimdb.sendData(result);
       
        else if (result.get(0).equalsIgnoreCase("show")
        && result.get(1).equalsIgnoreCase("charge"))
            iimdb.getData(result);
            
            
        else if (result.get(0).equals("quit")) return iimdb.getNodes(); 
       
        else
            System.out.println("Command not found");
       
       iimdb.showNodes();
        }
        
        catch(Exception e)
        {
            e.printStackTrace();
        }
        }
    }
    
    public ArrayList<Node> processStringForThirdParty(ArrayList<Node> nodes, String line)
    {
        String temp[];
        temp = line.split(" ");
        result = new ArrayList<String>();
        
        iimdb = new ModelDescriptionBuilder(nodes);
        for(int i=0; i<temp.length; i++)
        {  
           result.add(temp[i]);
        }
        if (result.get(0).equalsIgnoreCase("create")
        && (result.get(1).equalsIgnoreCase("node"))
        && (result.get(2).equalsIgnoreCase("value"))
        )
            iimdb.createNode(result.get(3),result.get(4), Integer.parseInt(result.get(5)), Integer.parseInt(result.get(6)),Double.parseDouble(result.get(7)));    
                    
        else  if (result.get(0).equalsIgnoreCase("create")
        && result.get(1).equalsIgnoreCase("node")
        && result.get(2).equalsIgnoreCase("default")
        )
       
            iimdb.createNode();
        
       
        else  if (result.get(0).equalsIgnoreCase("show")
        && result.get(1).equalsIgnoreCase("node")
        )
            iimdb.showNode(result.get(2));
        
        else if (result.get(0).equalsIgnoreCase("set")
        && result.get(1).equalsIgnoreCase("charge"))
             iimdb.sendData(result);
       
        else if (result.get(0).equalsIgnoreCase("show")
        && result.get(1).equalsIgnoreCase("charge"))
            iimdb.getData(result);
            
            
        else if (result.get(0).equals("quit")) return iimdb.getNodes(); 
       
        else
            System.out.println("Command not found");
       
        iimdb.showNodes();
        return iimdb.getNodes();
         }
    
    }
    
 
